package com.qk.management.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.entity.domain.customer.CustomerDO;
import com.qk.entity.Customer;
import com.qk.entity.dto.customer.CustomerListDTO;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author fantuan
 * @created 2025/10/16 17:17
 * @description
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Override
    public PageResult<CustomerDO> customerList(CustomerListDTO dto) {
        Page<Object> pages = new Page<>(dto.getPage(),dto.getPageSize());
        Page<CustomerDO> page = this.baseMapper.customerList(pages,dto);
        return PageResult.<CustomerDO>builder()
                .total(Long.valueOf(page.getTotal()).intValue())
                .rows(page.getRecords())
                .build();
    }

    @Override
    public void addCustomer(Customer customer) {
        if(ObjectUtil.isEmpty(customer.getName()) || ObjectUtil.isEmpty(customer.getPhone())){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(customer);
    }

    @Override
    public Customer selectById(Integer id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public void updateCustomerById(Customer customer) {
        if(ObjectUtil.isEmpty(customer.getName()) || ObjectUtil.isEmpty(customer.getPhone())){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.updateById(customer);
    }
}
